package com.leyou.order.controller;


import com.leyou.common.pojo.R;
import com.leyou.order.data.dto.CartGoodsDto;
import com.leyou.order.data.entity.OrderShoppingCart;
import com.leyou.order.mapper.OrderShoppingCartMapper;
import com.leyou.order.service.OrderShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xhh
 * @since 2022-07-05
 */
@RestController
@RequestMapping("/leyou.order/order-shopping-cart")
public class OrderShoppingCartController {
    @Autowired
    private OrderShoppingCartService orderShoppingCartService;
    @Autowired
    private OrderShoppingCartMapper orderShoppingCartMapper;

    /**
    * todo 加入购物车
    * @param orderShoppingCart
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/17 20:15
    * @deprecated
    */
    @RequestMapping(value = "/addCart",method = RequestMethod.POST)
    public R addCart(OrderShoppingCart orderShoppingCart){
        boolean save = orderShoppingCartService.save(orderShoppingCart);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }
    /**
    * todo 查看购物车
    * @param userId
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/17 21:01
    * @deprecated
    */
    @RequestMapping(value = "/seeCart",method = RequestMethod.POST)
    public R seeCart(@RequestParam("userId")Integer userId){
        List<CartGoodsDto> listByUserId = orderShoppingCartMapper.getListByUserId(userId);
        BigDecimal multiply=new BigDecimal(0);
        for (CartGoodsDto cartGoodsDto : listByUserId) {
            BigDecimal multiply1 = cartGoodsDto.getPrice().multiply(new BigDecimal(cartGoodsDto.getAmount()));
            //System.out.println(multiply1);
            multiply=multiply.add(multiply1);
        }
        return R.ok().data("list",listByUserId).data("totalPrice",multiply);
    }
    /**
    * todo 修改数量 或者 从购物车中删除
    * @param cartId
 * @param amount
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/17 21:05
    * @deprecated
    */
    @RequestMapping(value = "/changeAmount",method = RequestMethod.POST)
    public R changeAmount(@RequestParam("cartId")Integer cartId,@RequestParam("amount")Integer amount,@RequestParam("userId")Integer userId){
        if(amount>0) {
            OrderShoppingCart o = new OrderShoppingCart();
            o.setId(cartId);
            o.setAmount(amount);
            boolean b = orderShoppingCartService.updateById(o);
            if(b){
                R r = this.seeCart(userId);
                r.getData().remove("list");
                return r;
            }else{
                return R.error();
            }
        }else if(amount==0){
            boolean b = orderShoppingCartService.removeById(cartId);
            if(b){
                R r = this.seeCart(userId);
                r.getData().remove("list");
                return r;
            }else{
                return R.error();
            }
        }else{
            return R.error();
        }
    }
    /**
    * todo 直接购买
    * @param orderShoppingCart
    * @return com.leyou.common.pojo.R
    * @author xhh
    * @data 2022/7/19 17:04
    * @deprecated 
    */
    @RequestMapping(value = "/directPay",method = RequestMethod.POST)
    public R directPay(OrderShoppingCart orderShoppingCart){
        boolean save = orderShoppingCartService.save(orderShoppingCart);
        List<CartGoodsDto> listByCartId = orderShoppingCartMapper.getListByCartId(orderShoppingCart.getId());
        BigDecimal multiply=new BigDecimal(0);
        for (CartGoodsDto cartGoodsDto : listByCartId) {
            BigDecimal multiply1 = cartGoodsDto.getPrice().multiply(new BigDecimal(cartGoodsDto.getAmount()));
            multiply=multiply.add(multiply1);
        }
        System.err.println(orderShoppingCart.getId());
        boolean b = orderShoppingCartMapper.realDelete(orderShoppingCart.getId());
        return R.ok().data("list",listByCartId).data("totalPrice",multiply);
    }

}

